package cn.duwey.scs.service.user.service.impl;

import cn.duwey.scs.common.entity.UserEntity;
import cn.duwey.scs.common.exception.ValidationException;
import cn.duwey.scs.common.base.BaseRepository;
import cn.duwey.scs.service.user.dao.UserDao;
import cn.duwey.scs.service.user.repository.UserRepository;
import cn.duwey.scs.common.base.BaseServiceImpl;
import cn.duwey.scs.service.user.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends BaseServiceImpl<UserEntity> implements IUserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private UserRepository userRepository;

    @Override
    protected BaseRepository<UserEntity, Long> getRepository() {

        return userRepository;
    }

    @Override
    protected boolean passCheckBeforeInsert(UserEntity entity) {
        String username = entity.getUsername();

        if (StringUtils.isEmpty(username)) {
            throw new ValidationException("Username must not be empty!");
        } else {
            UserEntity userEntity = userRepository.findByUsername(username);

            if (userEntity != null) {
                throw new ValidationException("Username exists!");
            }
        }

        return true;
    }

}
